<?php

declare(strict_types=1);


namespace App\Controller;


use App\Request\Common\UploadRequest;
use App\Utils\Upload\Server;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

class UploadController extends AbstractController
{
    /**
     * @Inject()
     * @var Server
     */
    public $server;

    /**
     * @param UploadRequest $request
     * @return ResponseInterface
     */
    public function upload(UploadRequest $request)
    {
        $file = $this->request->file('file');
        $res = $this->server->upload($file);
        return $this->response->success([
            'id' => $res->getId(),
            'image' => $res->getImage()
        ]);
    }

    /**
     * @param string $id
     * @return mixed
     */
    public function view(string $id)
    {
        return di(Server::class)->view($id)->response();
    }

    /**
     * 下载
     * @param string $id
     * @return mixed
     */
    public function download(string $id)
    {
        return di(Server::class)->view($id)->downloadFile();
    }
}